package com.bjsxt.ego.common.util;

import org.apache.commons.codec.digest.DigestUtils;

public class EncryptUtil {

	/**
	 * 编码加密
	 * 密文:加密算法$随机数$加密(加密算法$随机数$明文)
	 * @param pass
	 * @return
	 */
	public static String encodeStr(String pass)
	{
		RegexUtil regexUtil =new RegexUtil();
	
		String encType="sha256";
		String randStr=regexUtil.proccedStr(32);
		
		/**
		 * 加密前置字符串 encStr
		 */
		String encStr=encType+"$"+randStr+"$";
		
		/*加密前字符串encStr+明文 */
		String encHead=encStr+pass;
		/*明文加密*/
		String encBack =DigestUtils.sha256Hex(encHead);
		return encStr+encBack;
	}
	
	public static boolean checkPass(String souPass,String encPass)
	{
		String [] encPassEs=encPass.split("\\$");
		/* 加密前置字符串*/
		String encStr=encPassEs[0]+"$"+encPassEs[1]+"$";
		/*加密前字符串*/
		String encHead=encStr+souPass;
		/*明文加密*/
		String encBack=DigestUtils.sha256Hex(encHead);
		encBack=encStr+encBack;
		return encBack.equals(encPass);
	}
}
